package com.example.logisticsingle.authorization;


import com.example.logisticsingle.Service.UserService;
import com.example.logisticsingle.String.StringUtils;
import com.example.logisticsingle.constants.PlatformTypeEnum;
import com.example.logisticsingle.constants.UserStatusEnum;
import com.example.logisticsingle.entity.SSOUser;
import com.example.logisticsingle.exception.UserDisabledException;
import com.example.logisticsingle.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * IAMUserDetailsService
 *
 */

@Component
@Slf4j
public class IAMUserDetailsService implements UserDetailsService {
    @Autowired
    private UserService userService;
    @Autowired
    HttpServletRequest request;
    @Override
    public UserDetails loadUserByUsername(String loginName) throws UsernameNotFoundException {
        String username = "";
        String platform = "";
        if(loginName.contains(LoginFilter.USERNAME_LOGINID_SPLIT)) {
            String args[]  = loginName.split(LoginFilter.USERNAME_LOGINID_SPLIT);
            platform = args[0];
            username = args[1];
        }else {
            username = loginName;
        }
        UserInfo user = userService.findUserByUsername(username);
        if(user==null) {
            UsernameNotFoundException e =  new UsernameNotFoundException("用户不存在");
            log.error(username+e.getMessage());
            throw e;
        }else {
            String platformType = user.getUserType();
            if(Integer.valueOf(user.getStatus()) != UserStatusEnum.ACTIVED.getCode()) {
                UserDisabledException e = new UserDisabledException("用户被限制登录");
                log.error(user+e.getMessage());
                throw e;
            }else if(StringUtils.isNull(platformType)||(!platform.equals(PlatformTypeEnum.getValueByCode(Integer.valueOf(platformType)))
                    &&PlatformTypeEnum.ALL.getCode()!=Integer.valueOf(platformType))) {
                UserDisabledException e = new UserDisabledException("该用户不允许在此平台登录");
                log.error(user+e.getMessage());
                throw e;
            }
        }
        //把角色放入认证器里
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        // TODO Auto-generated method stub
        //查找用户角色
        List<String> roles = userService.findUserRoles(user.getUserId());
        for(String role:roles) {
            authorities.add(new SimpleGrantedAuthority(role));
        }
        SSOUserDetail userDetail =  new SSOUserDetail(user.getUserName(), user.getPassword(), authorities);
        SSOUser ssoUser = new SSOUser();
        ssoUser.setUserid(user.getUserId());
        ssoUser.setUsername(username);
        ssoUser.setAvatar(user.getAvatar());
        ssoUser.setFullName(user.getFullName());
        ssoUser.setIsInspection(user.getIsInspection());
        ssoUser.setNickName(user.getAvatar());
        ssoUser.setSex(user.getSex());
        ssoUser.setRoles(roles);
        ssoUser.setUserType(user.getUserType());
        userDetail.setSsoUser(ssoUser);
        return userDetail;
    }
}
